#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import pdb



#using map_source file to generate digitwhu's map path infomation
#sql foramt
#INSERT INTO [digitwhu].[dbo].[path]
#           ([street_id]
#           ,[node_id]
#           ,[type]
#           ,[north]
#           ,[south]
#           ,[west]
#           ,[east])
#     VALUES
#           (<street_id, int,>
#           ,<node_id, int,>
#           ,<type, nvarchar(50),>
#           ,<north, int,>
#           ,<south, int,>
#           ,<west, int,>
#           ,<east, int,>)
#GO

#street_info foramt
#node_id pahts
WIDTH = 9

header = """
INSERT INTO [digitwhu].[dbo].[path]
           ([street_id]
           ,[node_id]
           ,[type]
           ,[north]
           ,[south]
           ,[west]
           ,[east])
     VALUES
"""

body = ''
f = open('path_info')
content = f.read()
lines = content.split('\n')
for line in lines:
	parts = line.split(' ')
	#没有path
	if( len(parts) == 1 ):
		continue
	node_id , directions = parts
	node_id = int(node_id)
	#单纯街道
	#print parts
	#pdb.set_trace()
	if( directions == 'ns' ):
		# street_id , node_id , type , north , south , west , east
		body += "(NULL,{0},'{1}',{2},{3},NULL,NULL),\n".format(node_id,directions,node_id-WIDTH,node_id+WIDTH)
		continue
	if( directions == 'we' ):
		# street_id , node_id , type , north , south , west , east
		body += "(NULL,{0},'{1}',NULL,NULL,{2},{3}),\n".format(node_id,directions,node_id-1,node_id+1)
		continue
	#十字路口
	for direction in directions:
		# street_id , node_id , type , north , south , west , east
		if( direction == 'n' ):
			body += "(NULL,{0},'{1}',{2},NULL,NULL,NULL),\n".format(node_id,direction,node_id-WIDTH)
		if( direction == 's' ):
			body += "(NULL,{0},'{1}',NULL,{2},NULL,NULL),\n".format(node_id,direction,node_id+WIDTH)
		if( direction == 'w' ):
			body += "(NULL,{0},'{1}',NULL,NULL,{2},NULL),\n".format(node_id,direction,node_id-1)
		if( direction == 'e' ):
			body += "(NULL,{0},'{1}',NULL,NULL,NULL,{2}),\n".format(node_id,direction,node_id+1)

	body += "(NULL,{0},'cross',NULL,NULL,NULL,NULL),\n".format(node_id)
		
#删除结尾逗号
body = body[0:-2]




footer = """
GO
"""

print header + body + footer
